define(function(require, exports, module) {

	var Backbone = require('backbone'),
	    moment = require('moment'),
	    _ = require('underscore'),
	    TimeModel = require('model/time');
	require('backbone.localStorage');

	/**
     * 时间集合
     */
	module.exports = Backbone.Collection.extend({
		model: TimeModel,
		localStorage: new Backbone.LocalStorage('time'),
		freeTime: function(time, exclude) {
			var now = parseInt(moment().format('X'));
			time = time || {};
			time.startAt = time.startAt || now;
			time.endAt = time.endAt || parseInt(moment('2099-12-31','YYYY-MM-DD').format('X'));
			//不是有效时间段
			if(!_.isNumber(time.startAt) || !_.isNumber(time.endAt) || time.startAt >= time.endAt) return [];

			//获取时间段列表
			if(this.length <= 0) this.fetch();
			var range = [{
				startAt: time.startAt,
				endAt: time.endAt
			}];
			//所有时间均未被预约
			if(this.length <= 0) return range;

			var inRange = [];
			//寻找与time相交的model
			this.each(function(model){
				//如果model被exclude包含，则排除
				if(exclude && model.equal(model.intersection(exclude))) return;
				if(model.intersection(time)) inRange.push(model.toJSON());
			});
			//如果不存在与time相交的model，则说明时间段空闲（可用）
			if(inRange.length <= 0) return range;
			//按开始时间排序
			inRange = _.sortBy(inRange, 'startAt');
			//使用以上过滤出的时间片段对目标时间段进行切割
			for(var i in inRange) {
				var temp = [];
				while(range.length>0) {
					var _time = new TimeModel(range.shift());
					//取交集
					var _intersection = _time.intersection(inRange[i]);
					if(!_intersection) {
						//没有交集
						temp.push(_time.toJSON());
					} else {
						//有交集
						if(!_time.equal(_intersection)) {
							//且不是被包含，则进行切割
							if(_intersection.startAt > _time.get('startAt')) {
								//开始段有空闲
								temp.push({
									startAt: _time.get('startAt'),
									endAt: _intersection.startAt
								});
							}

							if(_intersection.endAt < _time.get('endAt')) {
								//结束段有空闲
								temp.push({
									startAt: _intersection.endAt,
									endAt: _time.get('endAt')
								});
							}

							//如果时间片段被当前时间段包含，则不可能与其他时间段有交集
							if(_time.include(inRange[i])) break;
						}
					}
				}
				while(temp.length>0) {
					range.unshift(temp.pop());
				}
			}
			return range;
		}/*,
		fetchFreeTime: function(startAt, endAt) {
			var now = parseInt(moment().format('X'));
			startAt = startAt || now;
			endAt = endAt || parseInt(moment('2099-12-31','YYYY-MM-DD').format('X'));
			if(!_.isNumber(startAt) || !_.isNumber(endAt) || endAt<=startAt) {
				return [];
			}

			if(this.length <= 0) this.fetch();
			var range = [{
				startAt: startAt,
				endAt: endAt
			}];
			if(this.length <= 0) return range;

			var inRange = [],
			    _startAt = 0,
			    _endAt = 0;
			this.each(function(model){
				_startAt = model.get('startAt');
				_endAt = model.get('endAt');
				//1、结束时间落在区间内；2、开始时间落在区间内；3、吞没区间
				if(_endAt>startAt&&_endAt<=endAt ||
					_startAt>=startAt&&_startAt<endAt ||
					_endAt>=endAt&&_startAt<=startAt) {
					inRange.push(model.toJSON());
				}
			});
			if(inRange.length <= 0) return range;
			//按开始时间排序
			inRange = _.sortBy(inRange, 'startAt');
			for(var i in inRange) {
				var temp = [];
				_startAt = inRange[i].startAt;
				_endAt = inRange[i].endAt;
				while(range.length>0) {
					var obj = range.shift();
					//取交集
					var interStartAt = Math.max(_startAt, obj.startAt),
					    interEndAt = Math.min(_endAt, obj.endAt);
					if(interStartAt==obj.startAt && interEndAt==obj.endAt) {
						//被包含，则直接去掉
						continue;
					}
					if(interStartAt >= interEndAt) {
						//没有交集，则再添加回去
						temp.push(obj);
					}
					else {
						//有交集
						if(interStartAt>obj.startAt) {
							temp.push({
								startAt: obj.startAt,
								endAt: interStartAt
							});
						}

						if(interEndAt<obj.endAt) {
							temp.push({
								startAt: interEndAt,
								endAt: obj.endAt
							});
						}

						if(interStartAt>=obj.startAt && interEndAt<=obj.endAt) {
							//完全落在此区间内，不可能再落尽其他区间
							break;
						}
					}
				}
				while(temp.length>0) {
					range.unshift(temp.pop());
				}
			}
			return range;
		}*/
	});
});